<?php

//----------------------------------------------------------------------------------
function paramTest( $sParamName, $oResponse )
{
	if ( !isset( $_GET[ $sParamName ] ) )
	{
		$oResponse->error( 'missing "'.$sParamName.'" parameter for the server request' );
		return false;
	}
	return true;
}

//----------------------------------------------------------------------------------
function validatePath( $sPath )
{
	return true;
}

//----------------------------------------------------------------------------------
function commandLoadFile( $oResponse )
{
	//require_once( 'io.php' );
	if ( !paramTest( 'file', $oResponse ) ) return;
	if ( !validatePath( $_GET['file'] ) ) return;
	
	//$sExtension = FileManager::getFileExtension( $_GET[ 'file' ] );
	$sFilePath 		= $_SESSION[ 'sContentDir' ].'/'.$_GET['file'];
	/*if ( $sExtension == 'xml' )
	{
		$sXslDirPath 	= "../xsl";
		FileManager::readXmlContentFile( $oResponse, $sFilePath, $sXslDirPath );
	}
	else if ( $sExtension == 'jpg' || $sExtension == 'jpeg' || $sExtension == 'png' || $sExtension == 'gif' )
	{
		require_once( 'image_viewer.php' );
		ImageViewer::CreateImageContentPage( $oResponse, $_GET['file'] );
	}
	else if ( $sExtension == 'txt' )
	{
		require_once( 'txt_viewer.php' );
		TxtViewer::readContent( $oResponse, $_GET['file'] );		
	}*/
	require_once( 'content_dispatcher.php');
	$oContentDispatcher = new ContentDispatcher();
	$oContentDispatcher->readContent( $oResponse, $_GET['file'] );
}

//----------------------------------------------------------------------------------
function commandCreateFile( $oResponse )
{
	require_once( 'project.php' );
	if ( !paramTest( 'file', $oResponse ) ) return;
	if ( !validatePath( $_GET['file'] ) ) return;
	$sFilePath 		= $_SESSION[ 'sContentDir' ].'/'.$_GET['file'];
	
	// template
	$sTemplate = null;
	if ( isset( $_GET['template'] ) )
	{
		if ( !validatePath( $_GET['template'] ) ) return;
		$sTemplate = $_GET['template'];
	}
	
	$sProjectDir = FileManager::getRootDir( $_GET['file'] );
	$oProject = new Project();
	if ( !$oProject->setProjectDir( $sProjectDir, $oResponse ) )
	{
		$oResponse->error( 'Cannot get Project from directory : '.$sProjectDir );
		return;
	}
	
	FileManager::createContentFile( $oResponse, $sFilePath );
	
	$oProject->synchronizeProjectFromFileSystem( $oResponse );
}

//----------------------------------------------------------------------------------
function commandCreateDir( $oResponse )
{
	require_once( 'project.php' );
	if ( !paramTest( 'dir', $oResponse ) ) return;
	if ( !validatePath( $_GET['dir'] ) ) return;
	$sDirPath 		= $_SESSION[ 'sContentDir' ].'/'.$_GET['dir'];
	
	$sProjectDir = FileManager::getRootDir( $_GET['dir'] );
	$oProject = new Project();
	if ( !$oProject->setProjectDir( $sProjectDir, $oResponse ) )
	{
		$oResponse->error( 'Cannot get Project from directory : '.$sProjectDir );
		return;
	}
	
	FileManager::createContentDir( $oResponse, $sDirPath );
	
	$oProject->synchronizeProjectFromFileSystem( $oResponse );
}

//----------------------------------------------------------------------------------
function commandRename( $oResponse )
{
	//include( 'io.php' );
	require_once( 'project.php' );
	if ( !paramTest( 'path', $oResponse ) ) return;
	if ( !validatePath( $_GET['path'] ) ) return;
	$sPath 		= $_SESSION[ 'sContentDir' ].'/'.$_GET['path'];
	
	if ( !paramTest( 'new_name', $oResponse ) ) return;
	if ( !validatePath( $_GET['new_name'] ) ) return;
	$sNewName 		= $_GET['new_name'];
	
	$sProjectDir = FileManager::getRootDir( $_GET['path'] );
	$oProject = new Project();
	if ( !$oProject->setProjectDir( $sProjectDir, $oResponse ) )
	{
		$oResponse->error( 'Cannot get Project from directory : '.$sProjectDir );
		return;
	}
	
	FileManager::renameContentFileOrDir( $oResponse, $sPath, $sNewName );
	
	$oProject->synchronizeProjectFromFileSystem( $oResponse );
}

//----------------------------------------------------------------------------------
function commandRemoveFileOrDir( $oResponse )
{
  require_once( 'project.php' );
  if ( !paramTest( 'path', $oResponse ) ) return;
	if ( !validatePath( $_GET['path'] ) ) return;
  $sPath 		= $_SESSION[ 'sContentDir' ].'/'.$_GET['path'];
  
  $sProjectDir = FileManager::getRootDir( $_GET['path'] );
	$oProject = new Project();
	if ( !$oProject->setProjectDir( $sProjectDir, $oResponse ) )
	{
		$oResponse->error( 'Cannot get Project from directory : '.$sProjectDir );
		return;
	}
  
  FileManager::removeContentFileOrDir( $oResponse, $sPath );
  
  $oProject->synchronizeProjectFromFileSystem( $oResponse );
}

//----------------------------------------------------------------------------------
function commandRenameProject( $oResponse )
{
	require_once( 'project.php' );
}

//----------------------------------------------------------------------------------
function commandSynchronizeProject( $oResponse )
{
	require_once( 'project.php' );
	
	if ( !paramTest( 'dir', $oResponse ) ) return;
	
	$sProjectDir = $_GET[ 'dir' ];
	if ( !validatePath( $sProjectDir ) ) return;
	
	$oProject = new Project();
	if ( !$oProject->setProjectDir( $sProjectDir, $oResponse ) )
		$oResponse->error( 'Cannot get Project from directory : '.$sProjectDir );
	if ( $oProject->synchronizeProjectFromFileSystem( $oResponse ) )
		$oResponse->requestExecuted();
}

//----------------------------------------------------------------------------------
function commandUploadContentFile( $oResponse )
{
  require_once( 'io.php' );
  if ( !isset( $_FILES[ 'uploaded-content' ] ) )
  {
    $oResponse->error('Php error : The file was not uploaded');
    return;
  }
  $oUploadedContent = $_FILES[ 'uploaded-content' ];
  
  // Name
  if ( !isset( $oUploadedContent[ 'name' ] ) )
  {
    $oResponse->error('Cannot retrieve the original name of the uploaded file');
    return;
  }
  $sName = $oUploadedContent[ 'name' ];
  
  // sTmpName
  if ( !isset( $oUploadedContent[ 'tmp_name' ] ) )
  {
    $oResponse->error('Cannot retrieve the name of the temporary file corresponding to the uploaded file on the server');
    return;
  }
  $sTmpName = $oUploadedContent[ 'tmp_name' ];
  
  $sDirPath = FileManager::getContentPath( '' );
  
  FileManager::uploadContentFile( $oResponse, $sName, $sTmpName, $sDirPath );
}
?>